using System.Security.Cryptography;
using AutoMapper;
using ChikovMF.Application.Common.Mappings;
using ChikovMF.Application.Features.SHOLF;
using ChikovMF.Entities;

namespace ChikovMF.WebAPI.Models.Tags
{
    public class SHOLFDto : IMapWith<Tag>
    {
        public IFormFile File { get; set; } = null!;
        public Hash HashAlgorithm { get; set; }
        public int SegmentCount { get; set; }
        public string? ConnectionId { get; set; } = default!;

        public void Mapping(Profile profile)
        {
            profile.CreateMap<SHOLFDto, SHOLFCommand>()
                .ForMember(command => command.HashAlgorithm, opt => opt.MapFrom(dto => GetAlgorithm(dto.HashAlgorithm)));
        }

        public enum Hash
        {
            SHA256,
            SHA512,
            SHA1,
            MD5
        }

        private HashAlgorithm GetAlgorithm(Hash hashAlgorithm)
        {
            switch (hashAlgorithm)
            {
                case Hash.SHA256:
                    return SHA256.Create();
                case Hash.SHA512:
                    return SHA512.Create();
                case Hash.SHA1:
                    return SHA1.Create();
                case Hash.MD5:
                    return MD5.Create();
                default:
                    throw new ArgumentOutOfRangeException(nameof(hashAlgorithm), hashAlgorithm, null);
            }
        }
    }
}